package com.cw.jeeyt.service.component;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.cw.base.common.model.enums.EnumDatabaseType;
import com.cw.jeeyt.common.enums.EnumExportScriptKind;
import com.cw.jeeyt.service.intf.api.ApiDataGenenratorService;
import com.cw.jeeyt.service.converter.api.ApiDatabaseScriptConfigConverter;
import com.cw.jeeyt.common.utils.FileUtils;
import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.api.ApiDatabaseScriptConfigBo;
import com.cw.jeeyt.service.bo.api.ApiTableBo;
import com.cw.jeeyt.dao.model.api.ApiDatabaseDocDo;
import com.cw.jeeyt.dao.model.api.ApiDatabaseScriptConfigDo;
import com.cw.lang.common.utils.DateUtil;

import cn.hutool.core.util.ZipUtil;

/**
 * 数据库脚本导出组件
 *
 * @author: chenw
 * @create: 2018-12-19 21:06
 */
@Component
public class ApiExportDatabaseOracleScriptComponent {
    @Value("${database.export.outputPath}")
    private String databaseExportOutputPath;
    @Autowired
    private ApiDataGenenratorService apiDataGenenratorService;

    /**
     * 导出数据库脚本
     *
     * @throws Exception
     */
    public void exportOracleScript(ApiDatabaseDocDo apiDatabaseDocModel,
        ApiDatabaseScriptConfigDo apiDatabaseScriptConfigModel, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        List<ApiTableBo> apiTables = apiDataGenenratorService.generateData(apiDatabaseDocModel.getId(),
            apiDatabaseScriptConfigModel.getTablePrefix());
        if (CollectionUtils.isEmpty(apiTables)) {
            return;
        }
        //生成表缩写
        apiDataGenenratorService.generateTableAbbreviation(apiTables, apiDatabaseScriptConfigModel.getDatabaseId());
        //判断需要生成脚本种类 建表脚本、insert脚本、增量脚本
        String exportScriptKinds = apiDatabaseScriptConfigModel.getExportScriptKind();
        if (!StringUtils.isEmpty(exportScriptKinds)) {
            String scriptPath = databaseExportOutputPath + File.separator + DateUtil.getDate(new Date(),
                "yyyyMMddHHmmss");
            if (new File(scriptPath).exists()) {
                FileUtils.deleteDirectory(scriptPath);
            }
            FileUtils.createDirectory(scriptPath);
            //建表脚本
            if (exportScriptKinds.contains(EnumExportScriptKind.CREATE_SCRIPT.getCode())) {
                //组装建表脚本
                ApiDatabaseScriptConfigBo configBo = ApiDatabaseScriptConfigConverter.do2Bo(
                    apiDatabaseScriptConfigModel);
                apiDataGenenratorService.generateTableScript(scriptPath, apiTables, configBo, EnumDatabaseType.ORACLE);
            }
            //数据脚本
            if (exportScriptKinds.contains(EnumExportScriptKind.INSERT_SCRIPT.getCode())) {
                //组装数据脚本
                ApiDatabaseScriptConfigBo configBo = ApiDatabaseScriptConfigConverter.do2Bo(
                    apiDatabaseScriptConfigModel);
                apiDataGenenratorService.generateInsertScript(scriptPath, apiTables, configBo, EnumDatabaseType.ORACLE);
            }
            //下载文件
            File file = ZipUtil.zip(scriptPath);
            FileUtils.downFile(file, request, response);
        }
    }
}
